//
//  RegistrationViewController.m
//  project
//
//  Created by 黄建华 on 2018/1/8.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "RegistrationViewController.h"
#import <MOBFoundation/MOBFoundation.h>
#import "YJLocalCountryData.h"
#import "SectionsViewController.h"
#import "SNPersonSetViewController.h"
#import "BlueToothViewController.h"
#define CODECOUNTVALUE 60

@interface RegistrationViewController () <UITextFieldDelegate>

@property (nonatomic, strong) UILabel     *naTitle;
@property (nonatomic, strong) UITextField *textFiled1;
@property (nonatomic, strong) UITextField *textFiled2;
@property (nonatomic, strong) UITextField *textFiled3;
@property (nonatomic, strong) UITextField *textFiled4;

@property (nonatomic, assign) NSInteger      type;
@property (nonatomic, strong) UIButton       *areaButton;
@property (nonatomic, strong) NSMutableArray *areaArray;

@property (nonatomic, strong) UIButton  *loginButton;
@property (nonatomic, strong) UIButton  *codeButton;
@property (nonatomic, assign) NSInteger codeCount;
@property (nonatomic, assign) BOOL      isCodeSuccessFul;
@property (nonatomic, assign) BOOL      showInfo;

@end

@implementation RegistrationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = SNCOLORWHITE;
    
    _showInfo  = YES;
    _codeCount = CODECOUNTVALUE;
    
    CGFloat height = FitSimpleIpx(0, 20);
    
    UIView *StateView = [[UIView alloc] init];
    StateView.frame           = CGRectMake(0, 0, kSCREEN_WIDTH, 20 +height);
    StateView.backgroundColor = BBIGSTATECOLOR;
    [self.view addSubview:StateView];
    
    UIView *naView = [[UIView alloc] init];
    naView.frame           = SNFRAME(0, 20 +height, kSCREEN_WIDTH, 44);
    naView.backgroundColor = WELLGOCOLORWHITE;
    [self.view addSubview:naView];
    
    naView.layer.shadowColor   = WELLGOCOLORBLACK.CGColor;//shadowColor阴影颜色
    naView.layer.shadowOffset  = CGSizeMake(1, 1);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    naView.layer.shadowOpacity = 0.5;//阴影透明度，默认0
    naView.layer.shadowRadius  = 1;//阴影半径，默认3
    
    UIImageView *image = [[UIImageView alloc] init];
    image.frame = CGRectMake(10, 35, 21, 16);
    image.image = [[UIImage image:@"navjiantou.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    [image setTintColor:WELLGOCOLORBLACK];
    [self.view addSubview:image];
    
    UIButton *backButton = [[UIButton alloc] init];
    backButton.frame = CGRectMake(0, 20, 70, 44);
    [backButton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
    _naTitle               = [[UILabel alloc] initWithFrame:SNFRAME(0, 0, HHHWIDTH, 20)];
    _naTitle.center        = CGPointMake(HHHWIDTH/2, backButton.center.y);
    _naTitle.text          = SN_Text(@"注册");
    _naTitle.font          = SNSFONT(17.0);
    _naTitle.textAlignment = NSTextAlignmentCenter;
    _naTitle.textColor     = WELLGOCOLORBLACK;
    [self.view addSubview:_naTitle];
    
    _areaButton       = [[UIButton alloc] init];
    _areaButton.frame = CGRectMake(20, 80 + 60, 50, 35);
    
    UIView *view1 = [[UIView alloc] init];
    view1.frame               = CGRectMake(45, 80 + 60, kSCREEN_WIDTH - 90, 35);
    view1.backgroundColor     = WELLGOCOLORWHITE;
    view1.alpha               = 0.07;
    view1.layer.cornerRadius  = 18;
    view1.layer.masksToBounds = YES;
    [self.view addSubview:view1];
    
    UIView *view2 = [[UIView alloc] init];
    view2.frame               = CGRectMake(45, view1.y + 35 + 20, 130, 35);
    view2.backgroundColor     = WELLGOCOLORWHITE;
    view2.alpha               = 0.07;
    view2.layer.cornerRadius  = 18;
    view2.layer.masksToBounds = YES;
    [self.view addSubview:view2];
    
    UIView *view3 = [[UIView alloc] init];
    view3.frame               = CGRectMake(45, view2.y + 35 + 20, kSCREEN_WIDTH - 90, 35);
    view3.backgroundColor     = WELLGOCOLORWHITE;
    view3.alpha               = 0.07;
    view3.layer.cornerRadius  = 18;
    view3.layer.masksToBounds = YES;
    [self.view addSubview:view3];
    
    UIView *view4 = [[UIView alloc] init];
    view4.frame               = CGRectMake(45, view3.y + 35 + 20, kSCREEN_WIDTH - 90, 35);
    view4.backgroundColor     = WELLGOCOLORWHITE;
    view4.layer.cornerRadius  = 18;
    view4.alpha               = 0.07;
    view4.layer.masksToBounds = YES;
    [self.view addSubview:view4];
    
    UIImageView *image3 = [[UIImageView alloc] init];
    image3.image = [[UIImage image:@"passwordiamge.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    [image3 setTintColor:WELLGOCOLORRED];
    ;
    image3.frame = CGRectMake(view4.origin.x +18, view4.origin.y + 9, 12, 15);
    
    _textFiled4       = [[UITextField alloc] init];
    _textFiled4.frame = CGRectMake(_areaButton.origin.x + 38, _areaButton.origin.y +10 + 165, view1.width - 40, 15);
    [_textFiled4 changePlaceholder:SN_Text(@"请再次输入密码") withColor:HHHFONTCOLOR withFont:12.0];
    _textFiled4.textColor = WELLGOCOLORBLACK;
    _textFiled4.delegate  = self;
    [self.view addSubview:_textFiled4];
    _textFiled4.secureTextEntry = YES;
    
    UIView *view6 = [[UIView alloc] init];
    view6.frame               = CGRectMake(45, view4.y + 35 + 20 +15, kSCREEN_WIDTH - 90, 35);
    view6.backgroundColor     = WELLGOCOLORBLACK;
    view6.layer.cornerRadius  = 4;
    view6.layer.masksToBounds = YES;
    [self.view addSubview:view6];
    
    _loginButton                     = [[UIButton alloc] init];
    _loginButton.frame               = CGRectMake(45, view4.y + 35 + 20 +15, kSCREEN_WIDTH - 90, 35);
    _loginButton.layer.cornerRadius  = 18;
    _loginButton.layer.masksToBounds = YES;
    [_loginButton setTitleNormal:SN_Text(@"注册")];
    [_loginButton.titleLabel setFont:HHHFONTSIZE];
    [_loginButton addTarget:self action:@selector(loginButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_loginButton setTitleColorNormal:SNCOLORWHITE];
    [self.view addSubview:_loginButton];
    
    _textFiled1       = [[UITextField alloc] init];
    _textFiled1.frame = CGRectMake(_areaButton.origin.x + 40, _areaButton.origin.y +10, view1.width - 40, 15);
    if ([[ShareData sharedInstance].language hasPrefix:@"zh-Hans"]) {
        [_textFiled1 changePlaceholder:SN_Text(@"请输入手机/邮箱") withColor:HHHFONTCOLOR withFont:12.0];
    } else {
        [_textFiled1 changePlaceholder:SN_Text(@"请输入邮箱") withColor:HHHFONTCOLOR withFont:12.0];
    }
    
    _textFiled1.textColor = WELLGOCOLORBLACK;
    _textFiled1.delegate  = self;
    [self.view addSubview:_textFiled1];
    
    _textFiled2              = [[UITextField alloc] init];
    _textFiled2.keyboardType = UIKeyboardTypeNumberPad;
    _textFiled2.frame        = CGRectMake(_areaButton.origin.x + 38, _areaButton.origin.y +10 + 55, 130, 15);
    [_textFiled2 changePlaceholder:SN_Text(@"请输入验证码") withColor:HHHFONTCOLOR withFont:12.0];
    _textFiled2.textColor = WELLGOCOLORBLACK;
    _textFiled2.delegate  = self;
    [self.view addSubview:_textFiled2];
    
    _textFiled3       = [[UITextField alloc] init];
    _textFiled3.frame = CGRectMake(_areaButton.origin.x + 38, _areaButton.origin.y +10 + 110, view1.width - 40, 15);
    [_textFiled3 changePlaceholder:SN_Text(@"请输入密码") withColor:HHHFONTCOLOR withFont:12.0];
    _textFiled3.textColor = WELLGOCOLORBLACK;
    _textFiled3.delegate  = self;
    [self.view addSubview:_textFiled3];
    _textFiled3.secureTextEntry = YES;
    
    [self countrySetup];
    
    UIView *line1 = [[UIView alloc] init];
    line1.backgroundColor = SNCOLORGRAY;
    line1.alpha           = 0.33;
    line1.frame           = CGRectMake(_textFiled1.origin.x -5, _textFiled1.origin.y + _textFiled1.height + 5, _textFiled1.width +10, 1);
    [self.view addSubview:line1];
    
    UIView *line2 = [[UIView alloc] init];
    line2.backgroundColor = SNCOLORGRAY;
    line2.alpha           = 0.33;
    line2.frame           = CGRectMake(_textFiled2.origin.x -5, _textFiled2.origin.y + _textFiled2.height + 5, _textFiled2.width +10, 1);
    [self.view addSubview:line2];
    
    UIView *line3 = [[UIView alloc] init];
    line3.backgroundColor = SNCOLORGRAY;
    line3.alpha           = 0.33;
    line3.frame           = CGRectMake(_textFiled3.origin.x -5, _textFiled3.origin.y + _textFiled3.height + 5, _textFiled3.width +10, 1);
    [self.view addSubview:line3];
    
    UIView *line4 = [[UIView alloc] init];
    line4.backgroundColor = SNCOLORGRAY;
    line4.alpha           = 0.33;
    line4.frame           = CGRectMake(_textFiled4.origin.x -5, _textFiled4.origin.y + _textFiled4.height + 5, _textFiled4.width +10, 1);
    [self.view addSubview:line4];
    
    UIView *view5 = [[UIView alloc] init];
    view5.frame               = CGRectMake(line2.origin.x + line2.width, view1.y + 30 + 20, 110, 35);
    view5.backgroundColor     = WELLGOCOLORBLACK;
    view5.layer.cornerRadius  = 4;
    view5.layer.masksToBounds = YES;
    [self.view addSubview:view5];
    
    _codeButton                     = [[UIButton alloc] init];
    _codeButton.frame               = CGRectMake(line2.origin.x + line2.width, view1.y + 30 + 20, 110, 35);
    _codeButton.layer.cornerRadius  = 4;
    _codeButton.layer.masksToBounds = YES;
    [_codeButton setTitleNormal:SN_Text(@"获取验证码")];
    [_codeButton.titleLabel setFont:[UIFont systemFontOfSize:11.0]];
    [_codeButton addTarget:self action:@selector(codeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_codeButton setTitleColorNormal:SNCOLORWHITE];
    [self.view addSubview:_codeButton];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [_textFiled1 resignFirstResponder];
    [_textFiled2 resignFirstResponder];
    [_textFiled3 resignFirstResponder];
    [_textFiled4 resignFirstResponder];
    return YES;
}

// 注册按钮
- (void)loginButtonClick:(UIButton *)sender {
    [_loginButton setTitleColorNormal:HHHFONTCOLOR];
    [self performSelector:@selector(delayLoginButtonReset) withObject:nil afterDelay:0.1];
    NSLog(@"注册ButtonClick>>>>>");
    if ([_textFiled1.text containsString:@"@"]) {
        [self emailRegistration:sender]; // 邮箱注册
    } else{
        [self registration];//手机注册
    }
    
    // 成功之后进入用户信息
}
- (void)delayLoginButtonReset {
    [_loginButton setTitleColorNormal:WELLGOCOLORWHITE];
}
// 注册接口
- (void)registration {
    if (_textFiled1.text.length <= 0 || _textFiled2.text.length <= 0 || _textFiled3.text.length <= 0) {
        SHOWMBProgressHUD(SN_Text(@"信息请填写完整"), nil, nil, NO, 2.0);
        return;
    }
    if (_textFiled1.text.length != 11) {
        SHOWMBProgressHUD(SN_Text(@"电话格式错误"), nil, nil, NO, 2.0);
        return;
    }
    if (![_textFiled3.text isPassword]) {
        SHOWMBProgressHUD(SN_Text(@"密码必须为6～14位的数字或字母"), nil, nil, NO, 2.0);
        return;
    }
    if (![_textFiled4.text isEqualToString:_textFiled3.text]) {
        SHOWMBProgressHUD(nil, SN_Text(@"您输入的两个密码不匹配。"), nil, NO, 2);
        return;
    }
    
    [LOGINACCOUNT setObjectValue:_textFiled1.text];
    [LOGINPASSWORD setObjectValue:_textFiled3.text];
    [LOGINCODE setObjectValue:_textFiled2.text];
    [LOGINAREA setObjectValue:_areaButton.titleLabel.text];
    DEF_WEAKSELF_(RegistrationViewController)
    [SN_Net RegistratWithPhonesuccess:^(id object) {
        UserInfoModel *model = object;
        [KK_LastUser setObjectValue:model.user_id];
        [SN_PublicClass updateUser];
        SN_USER.loginAccount = _textFiled1.text;
        SN_USER.loginPassword = _textFiled3.text;
        SN_USER.is_first = model.is_first;
        SN_USER.user_id = model.user_id;
        SN_USER.access_token = model.access_token;
        SN_USER.registerDate = [NSDate date];
        [SN_USER updateToDB];

        if (model.is_first == 1) {
            NSLog(@"设置个人信息界面>>>>>");
        } else {
            NSLog(@"进入主页 并获取用户信息>>>>");
        }
        [weakSelf backClick];
    } fail:^(id object) {
    }];
}

// 邮箱注册接口
- (void)emailRegistration:(UIButton *)sender {
    if (_textFiled1.text.length <= 0 || _textFiled2.text.length <= 0 || _textFiled3.text.length <= 0) {
        SHOWMBProgressHUD(SN_Text(@"信息请填写完整"), nil, nil, NO, 2.0);
        return;
    }
    if (![self isValidateEmail:_textFiled1.text]) {
        SHOWMBProgressHUD(SN_Text(@"邮件格式不正确"), nil, nil, NO, 2.0);
        return;
    }
    if (![_textFiled3.text isPassword]) {
        SHOWMBProgressHUD(SN_Text(@"密码必须为6～14位的数字或字母"), nil, nil, NO, 2.0);
        return;
    }
    if (![_textFiled4.text isEqualToString:_textFiled3.text]) {
        SHOWMBProgressHUD(nil, SN_Text(@"您输入的两个密码不匹配。"), nil, NO, 2);
        return;
    }
    sender.enabled = NO;
    [LOGINACCOUNT setObjectValue:_textFiled1.text];
    [LOGINPASSWORD setObjectValue:_textFiled3.text];
    [LOGINCODE setObjectValue:_textFiled2.text];
    
    NSString     *passWordMd5 = [self md5:_textFiled3.text];
    NSDictionary *dict        = @{
                                  @"email":_textFiled1.text, @"password":passWordMd5,
                                  @"code":_textFiled2.text
                                  };
    [SN_Net RegistratWithEmailsuccess:^(id object)
     {
         if (object[@"ret"] && [object[@"ret"] intValue] == 0) {
             NSDictionary *dic = [object objectForKey:@"data"];
             UserInfoModel *model = [UserInfoModel parseString:dic.YYJSONString];
             [KK_LastUser setObjectValue:model.user_id];
             [SN_PublicClass updateUser];
             SN_USER.loginAccount = _textFiled1.text;
             SN_USER.loginPassword = _textFiled3.text;
             SN_USER.is_first = model.is_first;
             SN_USER.user_id = model.user_id;
             SN_USER.access_token = model.access_token;
             SN_USER.portrait = model.portrait;
             [SN_USER updateToDB];
             sender.enabled = YES;
             NSString *filePath = [[XYSandbox libCachePath] stringByAppendingPathComponent:KK_HeadImage];;
             NSData   *data     = [NSData dataWithContentsOfURL:[NSURL URLWithString:SN_USER.portrait]];
             [[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil];
             
             SNPersonSetViewController *vc = [[SNPersonSetViewController alloc] init];
             vc.type = 0;
             [self.navigationController pushViewController:vc animated:YES];
             
         } else {
            SHOWMBProgressHUD(nil, SN_Text(object[@"message"]), nil, NO, 2.0);
         }
         sender.enabled = YES;
     }fail:^(id object) {
         sender.enabled = YES;
     }];
}

//MD5加密
- (NSString *)md5:(NSString *)input {
    const char    *cStr = [input UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    
    CC_MD5(cStr, strlen(cStr), digest);   // This is the md5 call
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [output appendFormat:@"%02x", digest[i]];
    }
    
    return output;
}

- (void)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)codeButtonClick:(UIButton *)sender {
    [_textFiled1 resignFirstResponder];
    
    if (_codeCount == CODECOUNTVALUE) {
        if (_textFiled1.text == NULL) {
            if([[ShareData sharedInstance].language hasPrefix:@"zh-Hans"]){
                SHOWMBProgressHUD(SN_Text(@"邮箱或手机号不能为空"), nil, nil, NO, 2.0);
            }else{
               SHOWMBProgressHUD(SN_Text(@"邮箱不能为空"), nil, nil, NO, 2.0);
            }
            return;
        }
        
        if ([_textFiled1.text containsString:@"@"]) {
            if (![self isValidateEmail:_textFiled1.text]) {
                SHOWMBProgressHUD(SN_Text(@"邮件格式不正确"), nil, nil, NO, 2.0);
                return;
            }

            [self getCodeWithEmailClick];   // 邮箱验证码
        } else{
            if (![self valiMobile:_textFiled1.text]) {
                SHOWMBProgressHUD(SN_Text(@"电话格式错误"), nil, nil, NO, 2.0);
                return;
            }
            [self getCodeClick];// 手机注册
        }
        [self countDown];
    } else {
        return;
    }
}

- (void)countDown {
    if (_codeCount > 0) {
        _codeCount--;
        [self performSelector:@selector(countDown) withObject:nil afterDelay:1.0];
        NSString *codeS = [NSString stringWithFormat:@"%ld", _codeCount];
        [_codeButton setTitleNormal:codeS];
    }
    
    if (_codeCount == 0) {
        _codeCount = CODECOUNTVALUE;
        [_codeButton setTitleNormal:SN_Text(@"获取验证码")];
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(countDown) object:nil];
    }
}

// 获取邮箱的验证码
- (void)getCodeWithEmailClick {
    NSString *str = [NSString stringWithFormat:@"%@: %@", SN_Text(@"我们将向这封电子邮件发送验证码"), _textFiled1.text];
    
    NSLog(@"str>>>>%@", str);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:SN_Text(@"确认电子邮件")
                                                    message:str delegate:self
                                          cancelButtonTitle:SN_Text(@"取消")
                                          otherButtonTitles:SN_Text(@"确定"), nil];
    alert.tag = 100;
    [alert show];
}

- (BOOL)isValidateEmail:(NSString *)email {
    NSString    *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    
    return [emailTest evaluateWithObject:email];
}

- (void)getCodeClick {
   
    NSString *str = [NSString stringWithFormat:@"%@:%@ %@", SN_Text(@"将验证码发送到"), @"+86", _textFiled1.text];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:SN_Text(@"确定电话号码")
                                                    message:str delegate:self
                                          cancelButtonTitle:SN_Text(@"取消")
                                          otherButtonTitles:SN_Text(@"确定"), nil];
    alert.tag = 1;
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 1) {
        if (1 == buttonIndex) {
            NSDictionary *dict = @{ @"phone":_textFiled1.text, @"type":@(1), @"country_code":@"86"};
            //获取验证码
            AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
            manager.requestSerializer                 = [AFHTTPRequestSerializer serializer];
            NSString *url = @"/user/captcha";
            
            [manager GET:url parameters:dict success:^(NSURLSessionDataTask *_Nonnull task, id _Nonnull responseObject)
             {
                 if (responseObject[@"ret"] && [responseObject[@"ret"] intValue] == 0) {
                     SHOWMBProgressHUD(SN_Text(@"请求验证代码成功"), nil, nil, NO, 2.0);
                 } else {
                     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                         SHOWMBProgressHUD(nil, SN_Text(responseObject[@"message"]), nil, NO, 2.0);
                     });
                 }
                 _codeCount = CODECOUNTVALUE;
                 
                 [_codeButton setTitleNormal:SN_Text(@"获取验证码")];
                 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(countDown) object:nil];
             }   failure   :^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
                 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                     SHOWMBProgressHUD(nil, SN_Text([error localizedDescription]), nil, NO, 2.0);
                 });
                 _codeCount = CODECOUNTVALUE;
                 [_codeButton setTitleNormal:SN_Text(@"获取验证码")];
                 
                 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(countDown) object:nil];
             }];
        } else {
            NSLog(@"发送验证码取消按钮>>>>");
            
            _codeCount = CODECOUNTVALUE;
            [_codeButton setTitleNormal:SN_Text(@"获取验证码")];
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(countDown) object:nil];
        }
    }
    if (alertView.tag == 100) {
        if (1 == buttonIndex) {
            NSString *language = [NSString string];
            if ([[ShareData sharedInstance].language hasPrefix:@"zh-Hans"]) {
                language = @"cmn-Hans";
            } else {
                language = @"en";
            }
            // 邮件验证码
            NSDictionary *dict = @{ @"email":_textFiled1.text, @"type":@(1), @"language":language};
            
            
            AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
            manager.requestSerializer                 = [AFHTTPRequestSerializer serializer];
            NSString *url = @"/user/captcha";
            
            [manager GET:url parameters:dict success:^(NSURLSessionDataTask *_Nonnull task, id _Nonnull responseObject)
             {
                 if (responseObject[@"ret"] && [responseObject[@"ret"] intValue] == 0) {
                     SHOWMBProgressHUD(SN_Text(@"请求验证代码成功"), nil, nil, NO, 2.0);
                     _codeCount = CODECOUNTVALUE;
                     
                     [_codeButton setTitleNormal:SN_Text(@"获取验证码")];
                 } else {
                     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                         SHOWMBProgressHUD(nil, SN_Text(responseObject[@"message"]), nil, NO, 2.0);
                     });
                     _codeCount = CODECOUNTVALUE;
                     [_codeButton setTitleNormal:SN_Text(@"获取验证码")];
                     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(countDown) object:nil];
                 }
             }   failure   :^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
                 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                     SHOWMBProgressHUD(nil, SN_Text([error localizedDescription]), nil, NO, 2.0);
                 });
                 _codeCount = CODECOUNTVALUE;
                 [_codeButton setTitleNormal:SN_Text(@"获取验证码")];
                 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(countDown) object:nil];
                 SHOWMBProgressHUD(nil, SN_Text([error localizedDescription]), nil, NO, 2.0);
             }];
        } else {
            NSLog(@"发送验证码取消按钮>>>>");
            _codeCount = CODECOUNTVALUE;
            [_codeButton setTitleNormal:SN_Text(@"获取验证码")];
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(countDown) object:nil];
        }
    }
}

- (BOOL)valiMobile:(NSString *)mobile {
    if (mobile.length != 11) {
        return NO;
    }
    NSString    *MOBILE          = @"^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\\d{8}$";
    NSString    *CM              = @"^1(3[4-9]|4[7]|5[0-27-9]|7[08]|8[2-478])\\d{8}$";
    NSString    *CU              = @"^1(3[0-2]|4[5]|5[56]|7[0156]|8[56])\\d{8}$";
    NSString    *CT              = @"^1(3[3]|4[9]|53|7[037]|8[019])\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm     = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu     = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct     = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    if (([regextestmobile evaluateWithObject:mobile] == YES)
        || ([regextestcm evaluateWithObject:mobile] == YES)
        || ([regextestct evaluateWithObject:mobile] == YES)
        || ([regextestcu evaluateWithObject:mobile] == YES)) {
        return YES;
    } else {
        return NO;
    }
}

- (void)countrySetup {
    NSString *saveTimeString = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveDate"];
    
    NSDateComponents *dateComponents = nil;
    
    if (saveTimeString.length != 0) {
        dateComponents = [YJLocalCountryData compareTwoDays:saveTimeString];
    }
    if (dateComponents.day >= 1 || saveTimeString.length == 0) { //day = 0 ,代表今天，day =
    } else {
        _areaArray = [[MOBFDataService sharedInstance] cacheDataForKey:@"countryCodeArray" domain:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

